package com.aliang.shopping.model.converter;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aliang.shopping.model.dto.common.UpdateStatusDTO;
import com.aliang.shopping.model.dto.goods.GoodsEsDto;
import com.aliang.shopping.model.dto.goods.GoodsInsertDTO;
import com.aliang.shopping.model.dto.goods.GoodsPropertiesInsert;
import com.aliang.shopping.model.dto.goods.GoodsUpdateDTO;
import com.aliang.shopping.model.po.Goods;
import com.aliang.shopping.model.po.GoodsProperties;
import com.aliang.shopping.model.vo.goods.GoodsPropertiesVO;
import com.aliang.shopping.model.vo.goods.GoodsVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.Collections;
import java.util.List;

/**
 * 商品信息数据转换器
 */
@Mapper(componentModel = "Spring")
public interface GoodsConverter {

    /**
     * 得到主图集合中的第一个图片
     *
     * @param mainImages
     * @return
     */
    default String getOneMainImages(String mainImages) {
        List<String> list = getImageList(mainImages);
        return list.isEmpty() ? "" : list.get(0);
    }


    default String getImageJson(List<String> images) {
        if (images.isEmpty())
            return "[]";
        return JSONUtil.toJsonStr(images);
    }

    default List<String> getImageList(String str) {
        // 如果不是json格式，返回空数组
        if (StrUtil.isBlank(str) || !StrUtil.containsAll(str, "[", "]"))
            return Collections.emptyList();
        return JSONUtil.toList(str, String.class);
    }

    default String getTypeIdJson(List<Long> ids) {
        if (ids.isEmpty())
            return "[]";
        return JSONUtil.toJsonStr(ids);
    }

    default List<Long> getTypeIdList(String ids) {
        if (StrUtil.isBlank(ids))
            return Collections.emptyList();
        return JSONUtil.toList(ids, Long.class);
    }

    /**
     * 将商品信息插入数据传输对象转换为商品信息实体
     *
     * @param insertDTO
     * @return
     */
    @Mappings({
            @Mapping(target = "mainImages", expression = "java(getImageJson(insertDTO.getMainImages()))"),
            @Mapping(target = "imageDesc", expression = "java(getImageJson(insertDTO.getImageDesc()))"),
            @Mapping(target = "goodsName", expression = "java(insertDTO.getGoodsName())"),
            @Mapping(target = "typeId", expression = "java( getTypeIdJson(insertDTO.getTypeId()))")
    })
    Goods toPoByInsertDTO(GoodsInsertDTO insertDTO);

    /**
     * 将商品信息更新数据传输对象转换为商品信息实体
     *
     * @param updateDTO
     * @return
     */
    @Mappings({
            @Mapping(target = "mainImages", expression = "java(getImageJson(updateDTO.getMainImages()))"),
            @Mapping(target = "imageDesc", expression = "java(getImageJson(updateDTO.getImageDesc()))"),
            @Mapping(target = "typeId", expression = "java(getTypeIdJson(updateDTO.getTypeId()))"),
            @Mapping(target = "goodsName", expression = "java(updateDTO.getGoodsName())"),
    })
    Goods toPoByUpdateDTO(GoodsUpdateDTO updateDTO);

    /**
     * 将商品信息更新状态数据传输对象转换为商品信息实体
     *
     * @param updateDTO
     * @return
     */
    Goods toPoByUpdateStaticDTO(UpdateStatusDTO updateDTO);

    @Mappings({
            @Mapping(target = "mainImages", expression = "java(getImageList(goods.getMainImages()))"),
            @Mapping(target = "imageDesc", expression = "java(getImageList(goods.getImageDesc()))"),
            @Mapping(target = "typeId", expression = "java(getTypeIdList(goods.getTypeId()))"),
            @Mapping(target = "goodsName", expression = "java(goods.getGoodsName())")
    })
    GoodsVO toVo(Goods goods);

    /**
     * 将商品配置插入数据传输对象转换为商品配置实体
     *
     * @param goodsPropertiesInsert
     * @param goodsId
     * @return
     */
    @Mappings({
            @Mapping(target = "value", expression = "java(goodsPropertiesInsert.getValue())"),
            @Mapping(target = "name", expression = "java(goodsPropertiesInsert.getName())")
    })
    GoodsProperties getPropertiesPo(GoodsPropertiesInsert goodsPropertiesInsert, Long goodsId);

    @Mappings({
            @Mapping(target = "value", expression = "java(goodsProperties.getValue())"),
            @Mapping(target = "name", expression = "java(goodsProperties.getName())")
    })
    GoodsPropertiesVO getPropertiesVo(GoodsProperties goodsProperties);

    List<GoodsPropertiesVO> getPropertiesVoList(List<GoodsProperties> goodsPropertiesList);

    /**
     * 将商品po转换成EsDto
     */
    @Mappings({
            @Mapping(target = "goodsName", expression = "java(goods.getGoodsName())"),
            @Mapping(target = "mainImages", expression = "java(getOneMainImages(goods.getMainImages()))")
    })
    GoodsEsDto toEsDto(Goods goods);

    //获取一个商品主图

    List<GoodsEsDto> toEsDtoList(List<Goods> goodsList);
}
